package com.wanpinghui.supplier.addrbook;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

import com.githang.statusbar.StatusBarCompat;
import com.wanpinghui.supplier.R;
import com.wanpinghui.supplier.widgets.TitlePanelLayoutUser;
import com.wanpinghui.wphlibrary.WPHSdk;
import com.wanpinghui.wphlibrary.model.addrbook.AddrbookService;
import com.yunshl.yunshllibrary.qrcode.BaseQRCodeActivity;
import com.yunshl.yunshllibrary.qrcode.view.ViewfinderView;
import com.yunshl.yunshllibrary.utils.SystemUtil;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import retrofit.callback.YRequestCallback;


/***
 * 功能描述:扫一扫Activity
 * 作者:xiongning
 * 时间:2017/02/03
 * 版本:v1.0
 ***/
@ContentView(R.layout.layout_activity_scan)
public class ScanActivity extends BaseQRCodeActivity implements SurfaceHolder.Callback {
    public static final int TYPE_RETURN = 1;
    private int type;

    @ViewInject(R.id.ttl_about_bar)
    private TitlePanelLayoutUser mTitle;

    @ViewInject(R.id.sv_ltda_preview)
    private SurfaceView mSurfaceViewPreview;

    @ViewInject(R.id.viewfinder_view)
    private ViewfinderView mViewfinderView;

    @ViewInject(R.id.iv_scanning_wire)
    private ImageView mImageViewScanningWire;

    @ViewInject(R.id.iv_scanning_case)
    private ImageView mImageViewScanningCase;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                StatusBarCompat.setStatusBarColor(this, Color.WHITE, true);
            } else {
                if (SystemUtil.checkIsMeizuRom() || SystemUtil.isMIUI()) {
                    StatusBarCompat.setStatusBarColor(this, Color.WHITE, true);
                } else {
                    StatusBarCompat.setLightStatusBar(getWindow(), false);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        Window win = getWindow();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            WindowManager.LayoutParams winParams = win.getAttributes();
            final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
            winParams.flags &= ~bits;
            win.setAttributes(winParams);
        }

//        if (getIntent() != null) {
//            type = getIntent().getIntExtra("type", 0);
//        }
    }

    @Override
    public void scanResult(String resultString) {
//        if (type == TYPE_RETURN) {
//            Intent intent = new Intent();
//            intent.putExtra("result", resultString);
//            setResult(RESULT_OK,intent);
//
//            finish();
//        }else {
        if (resultString.contains("这是万屏汇的二维码")) {
            resultString = resultString.replace("这是万屏汇的二维码:", "");
            WPHSdk.getService(AddrbookService.class).addFriends(resultString, new YRequestCallback<Object>() {
                @Override
                public void onSuccess(Object var1) {
                    Intent intent = new Intent();
                    setResult(RESULT_OK, intent);
                    finish();
                }

                @Override
                public void onFailed(int var1, String message) {
                    //showWarningText("添加失败");
                    Intent intent = new Intent();
                    intent.putExtra("result", message);
                    setResult(0, intent);
                    finish();
                }

                @Override
                public void onException(Throwable var1) {

                    Intent intent = new Intent();
                    intent.putExtra("result", var1.getMessage());
                    setResult(0, intent);
                    finish();
                }
            });


        } else {
            Intent intent = new Intent();
            intent.putExtra("result", "扫描失败，请确保二维码是万屏汇的");
            setResult(0, intent);
            finish();
        }

//        }
    }

    @Override
    public SurfaceView getSurfaceView() {
        return mSurfaceViewPreview;
    }

    @Override
    public ViewfinderView getViewfinder() {
        return mViewfinderView;
    }

    @Override
    public void initEvints() {

        mTitle.setOnClickBacktrack(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                ScanActivity.this.finish();

            }
        });

        mImageViewScanningWire.post(new Runnable() {
            @Override
            public void run() {
                int height = mImageViewScanningCase.getMeasuredHeight() - mImageViewScanningWire.getMeasuredHeight();
                ObjectAnimator animator = ObjectAnimator.ofFloat(mImageViewScanningWire, "translationY", 0, height);
                animator.setDuration(3000);
                animator.setRepeatCount(ValueAnimator.INFINITE);
                animator.setRepeatMode(ValueAnimator.RESTART);
                animator.start();
            }
        });

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            ScanActivity.this.finish();
        }
        return false;
    }
}
